import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/core/utils/module_permissions_util.dart';
import 'package:enterprise_v6_app/app/core/values/images.dart';
import 'package:enterprise_v6_app/app/data/entities/application/securtity_base_drawer.dart';
import 'package:enterprise_v6_app/app/data/entities/application/smart_warehouse/warehouse_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/smart_warehouse/device_manger/device_manger_view.dart';
import 'package:enterprise_v6_app/app/modules/application/smart_warehouse/product_management/product_management_view.dart';
import 'package:enterprise_v6_app/app/modules/application/smart_warehouse/product_outbound/product_outbound_view.dart';
import 'package:enterprise_v6_app/app/modules/application/smart_warehouse/product_storage/product_storage_view.dart';
import 'package:enterprise_v6_app/app/modules/application/smart_warehouse/warehouse_management/warehouse_management_view.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class SmartWarehouseController extends GetxController {
  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 展开收起控制key
  final scaffoldKey = GlobalKey<ScaffoldState>();

  /// 仓库列表
  List<WarehouseEntity> warehouseList = [];

  /// 选中的页面
  int selectIndex = 0;

  /// 右侧筛选样式
  Widget? filterWidget;

  // 标题列表
  final titleList = [
    SecurityBaseEntity(
      title: '产品管理',
      icon: AppImages.icon_product_management,
      expandTitle: '产品管理',
      currentPageTitle: 0,
      pages: [ProductManagementPage()],
      path: '/productManagement',
    ),
    SecurityBaseEntity(
      title: '仓库管理',
      icon: AppImages.icon_warehouse_management,
      expandTitle: '仓库管理',
      currentPageTitle: 0,
      pages: [WarehouseManagementPage()],
      path: '/warehouseManagement',
    ),
    SecurityBaseEntity(
      title: '产品入库',
      icon: AppImages.icon_warehouse_outbound,
      expandTitle: '产品入库',
      currentPageTitle: 0,
      pages: [ProductStoragePage()],
      path: '/productStorage',
    ),
    SecurityBaseEntity(
      title: '产品出库',
      icon: AppImages.icon_warehouse_warehousing,
      expandTitle: '产品出库',
      currentPageTitle: 0,
      pages: [ProductOutboundPage()],
      path: '/productOutbound',
    ),
    SecurityBaseEntity(
      title: '报警管理',
      icon: AppImages.icon_alarm_management,
      expandTitle: '报警管理',
      currentPageTitle: 0,
      pages: [Container()],
      path: '/alarmManagement',
    ),
    SecurityBaseEntity(
      title: '设备管理',
      icon: AppImages.icon_alarm_management,
      expandTitle: '设备管理',
      currentPageTitle: 0,
      pages: [DeviceMangerPage()],
      path: '/deviceManagement',
    ),
  ];

  @override
  void onInit() {
    super.onInit();
    initModule();
    _getWarehouseList();
  }

  /// 根据后台权限初始化菜单
  void initModule() {
    ModulePermissionsUtil.removeRoleMenu(titleList: titleList, routes: AppRoutes.smartWarehouse);
    update();
  }

  /// 获取仓库列表
  void _getWarehouseList() async {
    // 获取仓库列表
    final res = await HttpUtil().generateGetRequest<List<WarehouseEntity>>(Interface.getWarehouseList).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        warehouseList = result.data!;
        update();
      }
    });
  }

  /// 点击左侧菜单
  /// [index] 索引
  void selectLeftMenu(int index) {
    selectIndex = index;
    update();
  }

  /// title选择切换
  /// [titleIndex] 标题索引
  /// [index] 页面索引
  void selectTitle(int titleIndex, int index) {
    titleList[titleIndex].currentPageTitle = index;
    update();
  }

  /// 点击展开菜单
  /// [index] 索引
  void selectExpandMenu(int index) {
    selectIndex = index;
    Get.back();
    update();
  }
}
